package portlet;

import javax.portlet.PortletMode;
import javax.portlet.WindowState;

import oracle.adf.model.binding.DCIteratorBinding;

import oracle.jbo.ViewCriteria;
import oracle.jbo.ViewCriteriaItem;
import oracle.jbo.ViewCriteriaRow;
import oracle.jbo.ViewObject;

import oracle.jbo.common.JboCompOper;

import oracle.portlet.bridge.adf.application.ADFBridgePortlet;

import portlet.resource.chuyenmucBundle;

public class chuyenmuc extends ADFBridgePortlet {
    private String langid;
    private String soid;
    private String chuyenmucid;
    private String kieutapchiId;
    private String urldanhsach;
    private String tenkieuanpham;
    private String sotapchi;
    private String ngayxuatban;
    private String viewRender;
    private boolean checkcount = false;
    //Labels
    private String labelChuyenmuc;
    private String labelSoanpham;

    public String getViewRender() {
        getLangid();
        getSoid();
        getSoMoiNhat();
        return viewRender;
    }

    public void getSoMoiNhat() {
        DCIteratorBinding soMoiNhatIter =
            Utils.getIteratorBinding("AptcSoMoiNhat1View1Iterator");
        ViewObject vo = soMoiNhatIter.getViewObject();
        ViewCriteria vc = vo.createViewCriteria();
        vc.setName("SoMoiNhatCriteria");
        ViewCriteriaRow vcRow = vc.createViewCriteriaRow();
        ViewCriteriaItem rowViewCriteriaItem =
            vcRow.ensureCriteriaItem("NgonNgu");
        rowViewCriteriaItem.setOperator(JboCompOper.OPER_EQ);
        rowViewCriteriaItem.setValue(langid);
        vc.addRow(vcRow);
        vo.getViewCriteriaManager().clearViewCriterias();
        vo.applyViewCriteria(vc);
        vo.executeQuery();
        if (vo.getAllRowsInRange().length > 0) {
            vo.setCurrentRowAtRangeIndex(0);
            soid = vo.getCurrentRow().getAttribute("Id").toString();
            kieutapchiId =
                    vo.getRowAtRangeIndex(0).getAttribute("KieutapchiId").toString();
            getChuyenmucTheoSo();
        }
        else{
            checkcount = false;        
        }
    }

    public void getChuyenmucTheoSo() {
        DCIteratorBinding soHienTaiIter =
            Utils.getIteratorBinding("AptcSoMoiNhat1View1Iterator");

        ViewObject vo1 = soHienTaiIter.getViewObject();
        ViewCriteria vc1 = vo1.createViewCriteria();
        vc1.setName("SoMoiNhat1ViewCriteria");
        ViewCriteriaRow vcRow1 = vc1.createViewCriteriaRow();
        ViewCriteriaItem rowViewCriteriaItem2 =
            vcRow1.ensureCriteriaItem("NgonNgu");
        rowViewCriteriaItem2.setOperator(JboCompOper.OPER_EQ);
        rowViewCriteriaItem2.setValue(langid);
        vcRow1.setConjunction(ViewCriteriaRow.VC_CONJ_AND);
        ViewCriteriaItem rowViewCriteriaItem3 =
            vcRow1.ensureCriteriaItem("KieutapchiId");
        rowViewCriteriaItem3.setOperator(JboCompOper.OPER_EQ);
        rowViewCriteriaItem3.setValue(kieutapchiId.toString());
        vc1.addRow(vcRow1);
        vo1.getViewCriteriaManager().removeApplyViewCriteriaName("SoMoiNhat1ViewCriteria");
        vo1.applyViewCriteria(vc1);
        vo1.setRangeSize(1);
        vo1.executeQuery();
        if (vo1.getAllRowsInRange().length > 0) {
            vo1.setCurrentRowAtRangeIndex(0);
            tenkieuanpham =
                    vo1.getCurrentRow().getAttribute("Kieutapchi").toString();
            sotapchi = vo1.getCurrentRow().getAttribute("Sotapchi").toString();
            ngayxuatban =
                    vo1.getCurrentRow().getAttribute("Ngayxuatban").toString();
            if (soid == null) {
                soid = vo1.getCurrentRow().getAttribute("Id").toString();
            }
            DCIteratorBinding soChuyenmucIter =
                Utils.getIteratorBinding("AptcSoChuyenmuc1View1Iterator");
            ViewObject vo2 = soChuyenmucIter.getViewObject();
            ViewCriteria vc2 = vo2.createViewCriteria();
            vc2.setName("SoChuyenmucViewCriteria");
            ViewCriteriaRow vcRow2 = vc2.createViewCriteriaRow();
            ViewCriteriaItem rowViewCriteriaItem4 =
                vcRow2.ensureCriteriaItem("AptcSoId");
            rowViewCriteriaItem4.setOperator(JboCompOper.OPER_EQ);
            rowViewCriteriaItem4.setValue(soid);
            vc2.addRow(vcRow2);
            vo2.getViewCriteriaManager().removeApplyViewCriteriaName("SoChuyenmucViewCriteria");
            vo2.applyViewCriteria(vc2);
            vo2.executeQuery();
            if(vo2.getAllRowsInRange().length>0)
                checkcount = true;
            else
                checkcount = false;
        }
    }

    public String getLangid() {
        langid = Utils.getPortalLocale("lang2").getLanguage();
        return langid;
    }

    public String getSoid() {
        try {
            soid = Utils.getPortletRequest().getParameter("so");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return soid;
    }

    public String getChuyenmucid() {
        try {
            chuyenmucid = Utils.getPortletRequest().getParameter("chuyenmuc");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return chuyenmucid;
    }

    public String getKieutapchiId() {
        return kieutapchiId;
    }

    public String getUrldanhsach() {
        try {
            urldanhsach =
                    Utils.getPortletRequest().getParameter("urldanhsach");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return urldanhsach;
    }

    public String getTenkieuanpham() {
        return tenkieuanpham;
    }

    public String getSotapchi() {
        return sotapchi;
    }

    public String getNgayxuatban() {
        return ngayxuatban;
    }

    public String getMonth() {
        return Utils.getResource("THANG", "lang2");
    }

    public String getLabelChuyenmuc() {
        labelChuyenmuc = Utils.getResource("TIEU_DE_CHUYEN_MUC", "lang2");
        return labelChuyenmuc;
    }

    public String getLabelSoanpham() {
        labelSoanpham = Utils.getResource("SO", "lang2");
        return labelSoanpham;
    }

    public void setCheckcount(boolean checkcount) {
        this.checkcount = checkcount;
    }

    public boolean isCheckcount() {
        return checkcount;
    }
}
